﻿Public Class ucCriteriaSelector

    Private _dataSource As ReportCriteriaDataSet

    Private masterBindingSource As New BindingSource()
    
    Public Property DataSource() As ReportCriteriaDataSet
        Get
            Return _dataSource
        End Get
        Set(ByVal value As ReportCriteriaDataSet)
            _dataSource = value

            Me.dgvReports.AutoGenerateColumns = False
            Me.dgvCriteria.AutoGenerateColumns = False

            ' Bind the master data connector to the Customers table.
            Me.masterBindingSource.DataSource = _dataSource
            Me.masterBindingSource.DataMember = "ReportDefinition"


        End Set
    End Property


    Private Sub ucCriteriaSelector_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Me.dgvReports.DataSource = Me.masterBindingSource


    End Sub

   
    Private Sub dgvReports_SelectionChanged(ByVal sender As Object, _
                                            ByVal e As System.EventArgs) _
                                        Handles dgvReports.SelectionChanged

        Me.dgvCriteria.DataSource = Nothing

        If Me.dgvReports.SelectedRows.Count = 0 Then
            Return
        End If

        Dim reportId As Integer = CInt(Me.dgvReports.SelectedRows(0).Cells(0).Value)
       Dim critChildRows As DataRow() = Me.DataSource.CriterionDefinition.Where(Function(dr)
                                                                                     Return dr.ReportId = reportId
                                                                                 End Function).ToArray

        If critChildRows.Count > 0 Then
            Me.dgvCriteria.DataSource = critChildRows.CopyToDataTable
        End If

    End Sub
End Class
